package com.zzty.txj.controller;

import com.zzty.txj.util.MessageUtil;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/wechat")
public class WeChatController {
    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(WeChatController.class);
    @Value("${wechat.token}")
    String token;
    @RequestMapping("/init")
    public String init(HttpServletRequest req)throws Exception{
        logger.info("==================================");
        String signature=req.getParameter("signature");
        String timestamp=req.getParameter("timestamp");
        String nonce=req.getParameter("nonce");
        String echostr=req.getParameter("echostr");
        System.out.println("initWeiChat timestamp="+timestamp);
        System.out.println("initWeiChat nonce="+nonce);
        System.out.println("initWeiChat signature="+signature);
        System.out.println("initWeiChat echostr="+echostr);

        System.out.println("token = "+token);

        return echostr;
    }

    @RequestMapping(value = "init",method = RequestMethod.POST)
    public String process(HttpServletRequest req, HttpServletResponse response) throws Exception{

        InputStream inst=req.getInputStream();
        Map map=new HashMap();
        SAXReader reader=new SAXReader();
        org.dom4j.Document doc=reader.read(inst);
        Element root=doc.getRootElement();
        List<Element> elements=root.elements();
        for(Element e: elements){
            String name=e.getName();
            String content=e.getText();
            map.put(e.getName(), e.getText());
        }

        //Map map=WeiChatUtil.xmlToMap(inst);
        System.out.println("FromUserName="+map.get("FromUserName"));
        System.out.println("Content="+map.get("Content"));
        System.out.println("MsgType="+map.get("MsgType"));
//        System.out.println("Location_X====="+map.get("Location_X"));
//        System.out.println("Location_Y====="+map.get("Location_Y"));
        Map mapres=new HashMap();
        logger.info("init====================================");
        String url="http://www.aboutit.com.cn/html/user.html";
        if(null==map.get("Content")){
            map.put("Content","欢迎来到自在天原,点击右下角进行绑定"+"<a href="+url+">"+"点击绑定"+"</a>");
        }
        mapres.put("FromUserName", map.get("ToUserName"));
        mapres.put("ToUserName", map.get("FromUserName"));
        mapres.put("Content",  "来自公众平台的回复:"+map.get("Content"));
        mapres.put("MsgType", "text");
        mapres.put("CreateTime", new Date().getTime());

        String res="<xml>"+
                "<ToUserName>"+mapres.get("ToUserName")
                + "</ToUserName>"
                + "<FromUserName>"+mapres.get("FromUserName")+"</FromUserName>"+

                "<CreateTime>"+mapres.get("CreateTime") +"</CreateTime><MsgType>"+mapres.get("MsgType")
                + "</MsgType><Content>"+mapres.get("Content")+"</Content>"+
                "</xml>";
        return res;

    }

    @RequestMapping("/getToken")
    public Map<String,String> getToken(){
        Map<String,String> maps=new HashMap<String,String>();
        return maps;
    }


}
